// file: Timer.c

#include <avr/io.h>         
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Timer.h"

volatile unsigned long cTimer2OVF = 0;

void ConfigureTimer2(void)
{
	// configure timer2:
	TCCR2A = 0;
	TCCR2B = (1<<CS20)|(1<<CS21)|(1<<CS22); // select F_CPU / 1024 as frequency
	TIMSK2 |= (1<<TOIE2);
	TCNT2 = 0;
	cTimer2OVF = 0;
	return;
}


void ResetTimer(void)
{
	TCNT2 = 0;
	cTimer2OVF = 0;
	return;
}

// get time in cycles: // get time in 1/10 ms:
unsigned long GetTime(void)
{
	unsigned char tmp_sreg;
	tmp_sreg = SREG; 
	cli();
	
	unsigned long iTemp = (cTimer2OVF << 8)+TCNT2;
	
	if(tmp_sreg & (128))
		sei();

	/*iTemp *= 10000;
	iTemp /= (F_CPU/1024);*/
	
	return iTemp;
}


// interrupt routines:
ISR(TIMER2_OVF_vect)
{
	cTimer2OVF++;	
}

